'use strict';
(function() {
	var excel = (function($) {
		var
		chooseList = {
			pt_id : $('#pt_id'),
			init : function() {
				this.choose();
				this.other_zc();
			},
			other_zc : function() {
				var input = $("input[value=4]");

				$(input).parent().append('<input name="ExcelEt[zcleixing1]" type="hidden" class="form-control other-zc" id="et-other_zc" value="" />');

				var other_zc = $("#et-other_zc")[0];

				if ($(input).is(":checked")) {
					$(other_zc).attr('type', 'text');
					$(other_zc).attr('value', $("#other_zc_value").val());
				}

				$(input).click(function() {
					if ($(this).is(":checked")) {
						$(other_zc).attr('type', 'text');
						$(other_zc).attr('value', '');
					} else {
						$(other_zc).attr('type', 'hidden');
						$(other_zc).attr('value', '');
					}
				});
			},
			choose : function() {
				var index = 0;
				$('#chooseButton').click(function() {
					index = layer.open({
					  type: 1,
					  title: '请选择要填写的表格类型：',
					  skin: 'layui-layer-rim', //加上边框
					  area: ['420px', '350px'], //宽高
					  content: '<div class="sample-list">' + 
			  					'<a class="btn btn-success" href="#" id="pdButton">1. PD</a>' + 
			  					'<a class="btn btn-success" href="/clinical/excel/create?type=et">2. ET</a>' + 
			  					'<a class="btn btn-success" href="/clinical/excel/create?type=sca">3. SCA</a>' + 
			  					'<a class="btn btn-success" href="/clinical/excel/create?type=cmt">4. CMT</a>' + 
			  					'<a class="btn btn-success" href="/clinical/excel/create?type=hsp">5. HSP</a>' + 
			  					'<a class="btn btn-success" href="/clinical/excel/create?type=pfbc">6. PFBC</a>' + 
			  					'<a class="btn btn-success" href="/clinical/excel/create?type=fcmte">7. FCMTE</a></div>',
					});
					$('#pdButton').click(function() {
						layer.close(index);
						layer.open({
						  type: 1,
						  title: '请选择要填写的表格类型：',
						  skin: 'layui-layer-rim', //加上边框
						  area: ['420px', '260px'], //宽高
						  content: '<div class="sample-list">' + 
				  					'<a class="btn btn-success" href="/clinical/excel/create?type=pd">PD初诊</a>' + 
				  					'<a class="btn btn-success" href="/clinical/excel/create?type=pdf">PD复诊</a>' + 
				  					'<a class="btn btn-success" href="/clinical/excel/create?type=udprs">美多芭冲击试验</a>' +
				  					'<a class="btn btn-success" href="/clinical/excel/create?type=udprs2">UDPRS</a>' +
				  					'<a class="btn btn-success" href="/clinical/excel/create?type=pdx">帕金森病患者营养状况横断面研究病例报告表</a></div>',
						});
						return false;
					});
					return false;
				});
			},
		},
		common = {
			submitButton : $('#submitButton'),
			form : $('#ExcelForm'),
			status : $("#status").val(),
			init : function() {
				helper.date_ymd();
				// this.submit();
				this.other();
			},
			// submit : function() {
			// 	var _this = this, msg;
			//
			// 	this.submitButton.click(function(e) {
			// 		if (_this.fieldTest()) {
			// 			return true;
			// 		} else {
			// 			return false;
			// 		}
			// 	});
			// },
			// fieldTest : function() {
			// 	var type = this.getType(),
			// 		inputs = $("input[type='text']"),
			// 		radioList = $(".radioList"),
			// 		selectList = $("select");
			//
			// 	if (type == 'pd') {
			// 		inputs.splice(0, 2);
			// 		inputs.splice(7, 306);
			// 		selectList = '';
			// 	} else if (type == 'pdf') {
			// 		inputs.splice(0, 2);
			// 		inputs.splice(4, 306);
			// 	} else if (type == 'pdx') {
			// 		return true;
			// 	}
			//
			// 	if (!helper.detect(inputs) || !helper.detectRadio(radioList) || !helper.detectSelect(selectList)) {
			// 		return false;
			// 	}
			//
			// 	return true;
			// },
			getType : function() {
				var type = location.href.match(/type=([a-z]*)/),
						thisType = (type != null) ? type[1] : '';
				
				return thisType;
			},
			other : function() {
				other.etInit();
				if (this.getType() == 'pfbc') {
					other.pfbc_memo();
				}
				if (this.getType() == 'pdx') {
					other.pdx_init();
				}
			}
		},
		other = {
			etInit : function() {
				var _this = this,
					status = $("#status").val();

				(/udprs/.test(status)) ? this.udprs_change() : this.et_change();
				
				$('select').click(function() {
					(/udprs/.test(status)) ? _this.udprs_change() : _this.et_change();
				});
			},
			et_change : function() {
				var total = 0;
				$('select').each(function() {
					total += (this.value != '') ? parseInt(this.value) : 0;
				});
				$('#totalScore').text(total);
			},
			udprs_change : function() {
				var on_total = 0,
						off_total = 0,
						result = 0;

				$('select').each(function() {
					if (/on/.test(this.id)) {

						on_total += (this.value != '') ? parseInt(this.value) : 0;
					} else if (/off/.test(this.id)) {

						off_total += (this.value != '') ? parseInt(this.value) : 0;
					}
				});

				if (off_total == 0) return ;
				result = parseInt((off_total - on_total) / off_total * 100);
				$('#totalScore').text(result);
				$('#offScore').text(off_total);
				$('#onScore').text(on_total);
			},
			pdx_init: function() {
				var _this = this;
				this.pdx_click(false);
				$('select').click(function() {
					_this.pdx_click(true);
				});
			},
			pdx_click : function(isSetContentHeight) {
				var part1_total = 0;
				var part2_total = 0;
				var count = 0;
				$('select').each(function() {
					if (this.id.match(/_[0-6]$/)) {
						count += (this.value != '') ? 1 : 0;
						part1_total += (this.value != '') ? parseFloat(this.value) : 0;
					} else {
						part2_total += (this.value != '') ? parseFloat(this.value) : 0;							
					}
				});
				$('#part1_total').text(part1_total);
				$('#_part1_total').text(part1_total);
				$('#part2_total').text(part2_total);
				$('#part1_part2_total').text(part1_total + part2_total);
				if (count >= 6 && part1_total <= 11) {
					$('#append_table').removeClass("hidden");
					$('#append_table').css('display', 'block');
					isSetContentHeight ? $('.events-content').css('height', '2250px') : '';
				} else if (!$('#append_table').hasClass("hidden")) {
					$('#append_table').addClass("hidden");
					isSetContentHeight ? $('.events-content').css('height', '772px') : '';
				}
			},
			pfbc_memo : function() {
				$(".memo").each(function() {
					if (/update/.test(location.href)) {
						if ($(this).find("input:checked").length == 0) {
							var thisInput = $(this).siblings("input"),
								thisOption = $(this).find("input[value=1]");
							
							$(thisInput).attr("type", "text");
							$(thisInput).attr("class", "form-control");
							$(thisOption).attr("checked", "true");
							$(thisOption).attr("disabled", "true");
						}
					}
				});
				$(".memo").change(function() {
					var thisValue = $(this).find("input:checked").val(),
						thisInput = $(this).siblings("input");

					if (thisValue == '1') {
						if ($(thisInput).val() == '0') {
							$(thisInput).val('');
						}
						$(thisInput).attr("type", "text");
						$(thisInput).attr("class", "form-control");
						$(this).find("input[value=1]").attr("disabled", "true");
					} else {
						$(thisInput).attr("type", "hidden");
						$(thisInput).attr("class", "");
						$(this).find("input[value=1]").removeAttr("disabled");
					}
				});
			}
		},
		init = function() {
			chooseList.init();
			common.init();
		};
		return {
			init : init
		};
	})(jQuery);

	excel.init();
})();